<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>富文本编辑器</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form" action="" style="padding:20px;">
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-block">
                    <input id="title" type="text" name="title" placeholder="此处输入标题" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">类别标签</label>
                <div class="layui-input-inline">
                    <input type="text" name="meta" placeholder="选择类别" autocomplete="off" class="layui-input" id="tag">
                </div>
            </div>
            <div id="editor" style="margin: 50px 0 50px 0">
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="save">保存</button>
                </div>
            </div>
        </form>

    </div>
</div>

<!-- 注意， 只需要引用 JS，无需引用任何 CSS ！！！-->
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../js/util.js"></script>
<script type="text/javascript">
    layui.use(['layer','wangEditor', 'tableSelect', 'table', 'form', 'miniTab'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            table = layui.table,
            form = layui.form,
            wangEditor = layui.wangEditor,
            tableSelect = layui.tableSelect,
            miniTab = layui.miniTab,
            userInfo = JSON.parse(getCookie("user"));

        var editor = new wangEditor('#editor');
        // editor.customConfig.pasteFilterStyle = false;
        // editor.customConfig.customAlert = function (info) {
        //     layer.msg(info);
        // };
        editor.create();

        var tag = null;
        tableSelect.render({
            elem: '#tag',
            searchKey: 'keyword',
            checkedKey: 'id',
            // searchPlaceholder: '自定义文字和name',
            table: {
                url: '/meta/getAll',
                cols: [[
                    { type: 'radio' },
                    { field: 'mid', title: 'ID', width: 100 },
                    { field: 'metaName', title: '名称', width: 300 }
                ]]
            },
            done: function (elem, data) {
                var NEWJSON = [];
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.metaName);
                    tag = item.id;
                });

                elem.val(NEWJSON.join(","))
            }
        });

        form.on("submit(save)", function (data) {
            var t = getTime();
            var postData = {
                "cid": null,
                "title": data.field.title,
                "meta": data.field.meta,
                "created": t,
                "modified": t,
                "text": editor.txt.html(),
                "commentNum": 0,
                "author": userInfo.userName,
                "status": "reviewed"
            };

            $.ajax({
                url: "/content/addContent",
                contentType: "application/json; charset=utf-8",
                method: "POST",
                data: JSON.stringify(postData),
                success: function (res) {
                    if (res.content) {
                        layer.msg("保存成功");
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.tableIns.reload();
                        }, 1000);
                    }
                },
                error: function (res) {
                    layer.msg("保存失败");
                    console.error(res);
                }
            });

            return false;
        });
    });
</script>
</body>
</html>